package cc.mrbird.system.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.AjaxResponse;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.system.domain.ProjectEntry;
import cc.mrbird.system.service.ProjectEntryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;



@Controller
public class ProjectEntryController extends BaseController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProjectEntryService projectEntryService;


    @Log("获取字典信息")
    @RequestMapping("projectEntry")
    @RequiresPermissions("projectEntry:list")
    public String index() {
        return "system/projectEntry/projectEntry";
    }


    @RequestMapping("projectEntry/list")
    @RequiresPermissions("projectEntry:list")
    @ResponseBody
    public Map<String, Object> projectEntryList(QueryRequest request, ProjectEntry projectEntry) {
        return super.selectByPageNumSize(request, () -> this.projectEntryService.findAllProjectEntry(projectEntry, request));
    }

    @Log("获取单个项目信息")
    @RequestMapping("projectEntry/get")
    @ResponseBody
    public ResponseBo getProjectEntry(ProjectEntry projectEntry) {
        try {
            ProjectEntry project = this.projectEntryService.getProjectEntry(projectEntry);
            return ResponseBo.ok(project);
        } catch (Exception e) {
            log.error("获取项目信息失败", e);
            return ResponseBo.error("获取项目信息失败，请联系网站管理员！");
        }
    }

    @Log("新增项目信息")
    @RequiresPermissions("projectEntry:add")
    @RequestMapping("projectEntry/add")
    @ResponseBody
    public ResponseBo addProjectEntry(ProjectEntry projectEntry) {
        try {
            this.projectEntryService.addProjectEntry(projectEntry);
            return ResponseBo.ok("新增项目信息成功！");
        } catch (Exception e) {
            log.error("新增项目信息失败", e);
            return ResponseBo.error("新增项目信息失败，请联系网站管理员！");
        }
    }

    @Log("修改项目信息")
    @RequiresPermissions("projectEntry:update")
    @RequestMapping("projectEntry/update")
    @ResponseBody
    public ResponseBo updateProjectEntry(ProjectEntry projectEntry) {
        try {
            this.projectEntryService.updateProjectEntry(projectEntry);
            return ResponseBo.ok("修改项目信息成功！");
        } catch (Exception e) {
            log.error("修改项目信息失败", e);
            return ResponseBo.error("修改项目信息失败，请联系网站管理员！");
        }
    }

    @Log("删除项目信息")
    @RequiresPermissions("projectEntry:delete")
    @RequestMapping("projectEntry/delete")
    @ResponseBody
    public ResponseBo deleteProjectEntry(String projectIds) {
        try {
            this.projectEntryService.deleteProjectEntry(projectIds);
            return ResponseBo.ok("删除项目信息成功！");
        } catch (Exception e) {
            log.error("删除项目信息失败", e);
            return ResponseBo.error("删除项目信息失败，请联系网站管理员！");
        }
    }

    @RequestMapping("projectEntry/excel")
    @ResponseBody
    public ResponseBo projectEntryExcel(ProjectEntry projectEntry) {
        try {
            List<ProjectEntry> list = this.projectEntryService.findAllProjectEntry(projectEntry, null);
            return FileUtil.createExcelByPOIKit("项目表", list, ProjectEntry.class);
        } catch (Exception e) {
            log.error("导出项目信息Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @RequestMapping("projectEntry/csv")
    @ResponseBody
    public ResponseBo projectEntryCsv(ProjectEntry projectEntry) {
        try {
            List<ProjectEntry> list = this.projectEntryService.findAllProjectEntry(projectEntry, null);
            return FileUtil.createCsv("项目表", list, ProjectEntry.class);
        } catch (Exception e) {
            log.error("导出项目信息Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }

    @RequestMapping(value = "projectEntry/importExcel", headers = "content-type=multipart/*", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse importProjectEntryExcel(@RequestParam("file") MultipartFile importFactory) {
        try {
            List<ProjectEntry> list = this.projectEntryService.importAllProjectEntry(importFactory);
            if (list.isEmpty()) {
                return new AjaxResponse(false, "上传数据为空，请重新选择文件");
            }
            return new AjaxResponse(true, "导入成功！");
        } catch (Exception e) {
            log.error("导入项目信息Excel失败", e);
            return new AjaxResponse(false, "导入Excel失败，请联系网站管理员！");
        }
    }


    /**
     * 下载excel模板
     *
     * @param response
     */
    @ResponseBody
    @RequestMapping("projectEntry/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response, HttpSession session) {
        try {
            InputStream inputStream = (InputStream) this.getClass().getClassLoader().getResourceAsStream("static/file/项目信息导入模板.xlsx");
            response.setContentType("application/zip");
            OutputStream out = response.getOutputStream();
            String fileName="项目信息导入模板.xlsx";
            response.setHeader("Content-Disposition", "attachment; filename = "+ java.net.URLEncoder.encode(fileName, "UTF-8"));
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("模板下载失败");
        }
    }



}
